import {
    ToolsItem
} from './toolsitem';

export class Tools {

    constructor(sf, toolsOptions) {
        this.sf = sf;
        this.init(toolsOptions);
    }

    init(toolsOptions) {
        this.title = toolsOptions.title;
        this.saveButtonText = toolsOptions.saveButtonText;
        this.saveFn = toolsOptions.saveFn;
        this.toolsItems = [];
        if (toolsOptions.item) {
            for (let item of toolsOptions.item) {
                this.toolsItems.push(new ToolsItem(this.sf, item));
            }
        }
    }

    getHtmlElement() {

        const tools = document.createElement('div');
        tools.className = 'sf-tools';

        const toolsTitle = document.createElement('div');
        toolsTitle.className = 'sf-tools-title';
        toolsTitle.innerHTML = this.title;
        tools.appendChild(toolsTitle);

        tools.appendChild(document.createElement('hr'));

        for (let i of this.toolsItems) {
            tools.appendChild(i.getDom());
        }
        tools.appendChild(document.createElement('hr'));

        const toolsSave = document.createElement('div');
        toolsSave.className = 'sf-tools-save';
        toolsSave.innerHTML = this.saveButtonText;
        toolsSave.addEventListener('click', e => {
            this.saveFn(this.sf.getJsonObject());
        });
        tools.appendChild(toolsSave);
        return tools;
    }

}